﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;

namespace CheckListProxy
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Thread[] checkerThread;
        private int beenChecked;
        private int checking;
        private bool isRunning;

        private void filterProxy(int index, bool Valid)
        {
            try
            {
                if (Valid)
                {
                    this.lsbCheckedProxy.Items.Add(this.lsbListProxy.Items[index]);
                    this.lsbListProxy.Items.RemoveAt(index);
                }
                else
                {
                    this.lsbListProxy.Items.RemoveAt(index);
                }
            }
            catch
            {
            }
        }

        private delegate void filterProxyHandler(int Index, bool Valid);

        private delegate void updateApplicationDel(string Status);

        private void updateApplication(string status)
        {
            this.lblResponding.Text = "Responding proxies : " + this.lsbCheckedProxy.Items.Count.ToString();
            this.txtStatus.Text = status;
        }

        public void checkProxys(object Index)
        {
            int index = (int)Index;
            while (this.isRunning)
            {
                if (this.beenChecked >= this.checking)
                {
                    this.isRunning = false;
                }
                if (index >= this.lsbListProxy.Items.Count)
                {
                    this.checkerThread[index].Abort();
                }
                string str = this.lsbListProxy.Items[index].ToString();
                object[] args = new object[] { index, true };
                object[] objArray2 = new object[] { index, false };
                try
                {
                    WebProxy proxy = new WebProxy(str.Split(new char[] { ':' })[0], Convert.ToInt32(str.Split(new char[] { ':' })[1]));
                    WebRequest request = WebRequest.Create(this.txtServer.Text.ToString());
                    request.Proxy = proxy;
                    request.Timeout = Convert.ToInt32(this.txtTimeout.Text);
                    Console.WriteLine(proxy.Address.ToString());
                    Console.WriteLine(request.RequestUri.AbsoluteUri.ToString());
                    request.GetResponse();
                    if (this.lsbListProxy.InvokeRequired)
                    {
                        this.lsbListProxy.Invoke(new filterProxyHandler(this.filterProxy), args);
                    }
                    else
                    {
                        this.filterProxy(index, true);
                    }
                    if (this.txtStatus.InvokeRequired)
                    {
                        this.txtStatus.Invoke(new updateApplicationDel(this.updateApplication), new object[] { string.Concat(new object[] { "Processed ", this.beenChecked, " out of", this.checking }) });
                    }
                    else
                    {
                        this.txtStatus.Text = string.Concat(new object[] { "Processed ", this.beenChecked, " out of", this.checking });
                    }
                    this.beenChecked++;
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                    if (this.lsbListProxy.InvokeRequired)
                    {
                        this.lsbListProxy.Invoke(new filterProxyHandler(this.filterProxy), objArray2);
                    }
                    else
                    {
                        this.filterProxy(index, false);
                    }
                    if (this.txtStatus.InvokeRequired)
                    {
                        this.txtStatus.Invoke(new updateApplicationDel(this.updateApplication), new object[] { string.Concat(new object[] { "Processed ", this.beenChecked, " out of", this.checking }) });
                    }
                    else
                    {
                        this.txtStatus.Text = string.Concat(new object[] { "Processed ", this.beenChecked, " out of", this.checking });
                    }
                    this.beenChecked++;
                }
            }
            this.checkerThread[1].Abort();
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            int num2 = 0;
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Filter = "Text Files|*.txt|All Files|*.*";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    int length = System.IO.File.ReadAllLines(dialog.FileName).Length;
                    foreach (string str in System.IO.File.ReadAllLines(dialog.FileName))
                    {
                        try
                        {
                            this.lsbListProxy.Items.Add(str);
                        }
                        catch
                        {
                            num2++;
                        }
                        num2++;
                        this.txtStatus.Text = string.Concat(new object[] { "On line: ", num2, " out of: ", length });
                        Application.DoEvents();
                    }
                }
            }
        }

        private void btnClear1_Click(object sender, EventArgs e)
        {
            this.lsbListProxy.Items.Clear();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog dialog = new SaveFileDialog())
            {
                dialog.Filter = "Text Files|*.txt|All Files|*.*";
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    int count = this.lsbCheckedProxy.Items.Count;
                    foreach (string str in this.lsbCheckedProxy.Items)
                    {
                        try
                        {
                            System.IO.File.WriteAllLines(dialog.FileName, this.lsbCheckedProxy.Items.Cast<string>());
                        }
                        catch (Exception exception)
                        {
                            MessageBox.Show(exception.ToString());
                        }
                    }
                }
            }
        }

        private void btnClear2_Click(object sender, EventArgs e)
        {
            this.lsbCheckedProxy.Items.Clear();
            this.lblResponding.Text = "Responding proxies : 0";
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            this.checkerThread = new Thread[Convert.ToInt32(this.txtThread.Text)];
            this.checking = this.lsbListProxy.Items.Count;
            this.txtStatus.Text = "Starting to filter.";
            this.isRunning = true;
            this.beenChecked = 1;
            for (int i = 0; i <= (Convert.ToInt32(this.txtThread.Text) - 1); i++)
            {
                int parameter = i;
                this.checkerThread[i] = new Thread(new ParameterizedThreadStart(this.checkProxys));
                this.checkerThread[i].Start(parameter);
            }
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            this.isRunning = false;
            for (int i = 0; i <= (Convert.ToInt32(this.txtThread.Text) - 1); i++)
            {
                this.checkerThread[i].Abort();
            }
        }

    }
}
